package com.example.arithmeticleetcode.learnArithmetic.stack;

/**
 * @program: arithmetic-leetcode
 * @description: 浏览器的前进与后退        软件的撤销与恢复
 * @author: FangZhen
 * @create: 2020-08-20 14:58
 **/
public class StackExercise01 {

    public static void main(String[] args) {
        Browser browser = new Browser();
        browser.push("www.1.com");
        browser.push("www.2.com");
        browser.push("www.3.com");
        System.out.println(browser);

        System.out.println(browser.forward()); // null
        System.out.println(browser.drawback()); //www.2.com
        System.out.println(browser.forward()); //www.3.com
        System.out.println(browser.drawback()); //www.2.com
        browser.push("www.4.com");
        System.out.println(browser.forward());//null
        System.out.println(browser.drawback());//www.2.com
        System.out.println(browser.forward());//www.4.com
    }


}


class Browser {

    private Stack<String> currrentStack = new Stack<>();
    private Stack<String> reverseStack = new Stack<>();

    public String forward () {
        if (reverseStack.isEmpty()) {
            return null;
        }
        String pop = reverseStack.pop();
        currrentStack.push(pop);
        return pop;
    }

    public String drawback() {
        if (currrentStack.isEmpty()) {
            return null;
        }
        String pop = currrentStack.pop();
        reverseStack.push(pop);
        return currrentStack.top();
    }

    public void push(String str) {
        currrentStack.push(str);
        while (!reverseStack.isEmpty()) {
            reverseStack.pop();
        }
    }
}
